package cn.cj;


public class BloomFilters {


    private  int arraysize;

    private  int []array;

    public BloomFilters(int arraysize) {
        this.arraysize = arraysize;
        this.array = new int[arraysize];
    }

    public void add(String key)
    {
        int first = hashcode_1(key);
        int second= hashcode_2(key);
        int third = hashcode_3(key);
        array[first%arraysize]=1;
        array[second%arraysize]=1;
        array[third%arraysize]=1;
    }


    public boolean check(String key)
    {

        int first = hashcode_1(key);
        int second= hashcode_2(key);
        int third = hashcode_3(key);

        int firstIndex=array[first%arraysize];
        if(firstIndex==0)
            return false;
        int secondIndex=array[second%arraysize];
        if(secondIndex==0)
            return false;
        int thirdIndex=array[third%arraysize];
        if(thirdIndex==0)
            return false;
        return true;

    }

    private int hashcode_3(String key) {

        int hash=0;
        for(int i=0;i<key.length();i++)
        {
            hash+=key.charAt(i);
            hash+=(hash<<10);
            hash^=(hash>>6);
        }
        hash+=hash<<3;
        hash^=hash>>11;
        hash+=hash<<15;
        return  Math.abs(hash);

    }


    private int hashcode_2(String key) {

        final  int p=16777777;
        int hash=(int) 2166136266L;
        for(int i=0;i<key.length();i++)
            hash=(hash^key.charAt(i))*p;
        hash+=hash<<13;
        hash^=hash>>7;
        hash+=hash<<3;
        hash^=hash>>17;
        hash+=hash<<5;
        return  Math.abs(hash);

    }


    private int hashcode_1(String key) {
            int hash=0;
            for(int i=0;i<key.length();i++)
            hash=33*hash+key.charAt(i);
            return  Math.abs(hash);
    }
}
